package com.wangzhixuan.commons.utils.word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;

import com.wangzhixuan.commons.base.ApiBaseController;
import com.wangzhixuan.commons.utils.UUIDGenerator;
import com.wangzhixuan.commons.utils.date.DateUtil;
import com.wangzhixuan.model.vo.ExcelJobInfomationVo;
import com.wangzhixuan.model.wordVo.WordAnalysisVo;
import com.wangzhixuan.model.wordVo.WordMeetingVo;
import com.wangzhixuan.model.wordVo.WordPeopleVo;
import com.wangzhixuan.model.wordVo.WordSurveyVo;

import sun.misc.BASE64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * 用FTL模板生产WORD
 * @author GTE
 *
 */
public class CreatWordFtl{

    @SuppressWarnings("unused")
	public String exportListWord(ExcelJobInfomationVo excelJobInfo,WordSurveyVo wdSurvey
			 ,WordMeetingVo wordMeetingVo,List<WordAnalysisVo> wordAnalysisVo,List<String> wdPhenom,
			 List<WordPeopleVo> wordPeopleVo,List<String> imgPath,String getRootPath,String getbaseUrl) throws Exception{  
        //构造数据  
    	 DocUtil docUtil=new DocUtil();
	        Map<String, Object> dataMap=new HashMap<String, Object>();
	        SimpleDateFormat DatetoString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        dataMap.put("A", excelJobInfo.getJobName()==null?"":excelJobInfo.getJobName());
	        dataMap.put("B", excelJobInfo.getJobDescribe()==null?"":excelJobInfo.getJobDescribe());
	        dataMap.put("C", excelJobInfo.getJobCategory()==null?"":excelJobInfo.getJobCategory());
	        dataMap.put("D", excelJobInfo.getAddress()==null?"":excelJobInfo.getAddress());
	        dataMap.put("E", excelJobInfo.getStatus()==null?"":excelJobInfo.getStatus());
	        dataMap.put("F", excelJobInfo.getCreatName()==null?"":excelJobInfo.getCreatName());
	        dataMap.put("G", DatetoString.format(excelJobInfo==null?new Date():excelJobInfo.getEndTime()));
	        //dataMap.put("H", excelJobInfo.getCreatPhone());                      
	        dataMap.put("I", DatetoString.format(excelJobInfo==null?new Date():excelJobInfo.getInfaceEndTime()));
	        dataMap.put("J", DatetoString.format(excelJobInfo==null?new Date():excelJobInfo.getStartTime()));
	        dataMap.put("K", DatetoString.format(wdSurvey==null?new Date():wdSurvey.getSurveyTime()));
	        System.out.println(wdSurvey);
	        if(wdSurvey==null){
		        dataMap.put("L", "");
		        dataMap.put("M", "");
		        dataMap.put("N", "");
		        dataMap.put("O", "");
		        dataMap.put("P", "");
	        }
	        else{
		        dataMap.put("L", wdSurvey.getSurveyName()==null?"":wdSurvey.getSurveyName());
		        dataMap.put("M", wdSurvey.getSurveyContent()==null?"":wdSurvey.getSurveyContent());
		        dataMap.put("N", wdSurvey.getBlackoutRange()==null?"":wdSurvey.getBlackoutRange());
		        dataMap.put("O", wdSurvey.getSecurity()==null?"":wdSurvey.getSecurity());
		        dataMap.put("P", wdSurvey.getUserName()==null?"":wdSurvey.getUserName());
	        }

	        if(wordMeetingVo==null){
        		dataMap.put("Q", "未开始");
        		dataMap.put("R", "未开始");
	        }
	        else{
	        	if("11".equals(wordMeetingVo.getType())){
		        	if("22".equals(wordMeetingVo.getStatus())){
		        		dataMap.put("Q", "未结束");
		        		dataMap.put("R", "未开始");
		        	}
		        	if("23".equals(wordMeetingVo.getStatus())){
		        		dataMap.put("Q", "已结束");
		        		dataMap.put("R", "已结束");
		        	}
		        	
		        }else if("12".equals(wordMeetingVo.getType())){
		        	if("22".equals(wordMeetingVo.getStatus())){
		        		dataMap.put("Q", "已结束");
		        		dataMap.put("R", "未结束");
		        	}
		        	if("23".equals(wordMeetingVo.getStatus())){
		        		dataMap.put("Q", "已结束");
		        		dataMap.put("R", "已结束");
		        	}
		        }else{
	     		dataMap.put("Q", "已结束");
	     		dataMap.put("R", "已结束");
		        }
	        }
	        String listWdPhenom = "";
	        String Reason = "";
	        String Method = "";
	        String Status = "";
	        String CreatTime= "";
	        String uName = "";
	        for(int i=0;i<wdPhenom.size();i++){
	        	listWdPhenom = listWdPhenom+"【现象"+(i+1)+"】:"+wdPhenom.get(i)+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Reason = Reason+"【原因"+(i+1)+"】:"+wordAnalysisVo.get(i).getReason()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Method = Method+"【方法"+(i+1)+"】:"+wordAnalysisVo.get(i).getMethod()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	Status = Status+"【问题"+(i+1)+"】:"+wordAnalysisVo.get(i).getStatus()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	uName = uName+"【负责人"+(i+1)+"】:"+wordAnalysisVo.get(i).getuName()+";  ";
	        }
	        for(int i=0;i<wordAnalysisVo.size();i++){
	        	CreatTime = CreatTime+"【日期"+(i+1)+"】:"+wordAnalysisVo.get(i).getCreatTime()+";  ";
	        }
	        dataMap.put("S",listWdPhenom);
	        dataMap.put("T",Reason);
	        dataMap.put("U",Method);
	        dataMap.put("V",Status);
	        dataMap.put("W",uName);
	        dataMap.put("X",CreatTime);
	        String pName = "";
	        String pCreatTime= "";
	        String address = "";
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	pName = pName+"【"+(i+1)+"】:"+wordPeopleVo.get(i).getuName()+";  ";
	        }
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	pCreatTime = pCreatTime+"【时间"+(i+1)+"】:"+wordPeopleVo.get(i).getCreatTime()+";  ";
	        }
	        for(int i=0;i<wordPeopleVo.size();i++){
	        	address = address+"【地址"+(i+1)+"】:"+wordPeopleVo.get(i).getAddress()+";  ";
	        }
	        dataMap.put("Y",pName);
	        dataMap.put("Z",pCreatTime);
	        dataMap.put("A2",address);
	        if(imgPath.size()>=3){
	        	dataMap.put("image", getImageStr(imgPath.get(0),getRootPath,getbaseUrl));
	 	        dataMap.put("image2", getImageStr(imgPath.get(1),getRootPath,getbaseUrl));
	 	        dataMap.put("image3", getImageStr(imgPath.get(2),getRootPath,getbaseUrl));
	        }
	        if(imgPath.size()==2){
	        	dataMap.put("image", getImageStr(imgPath.get(0),getRootPath,getbaseUrl));
	 	        dataMap.put("image2", getImageStr(imgPath.get(1),getRootPath,getbaseUrl));
	 	        dataMap.put("image3", "");
	        }
	        if(imgPath.size()==1){
	        	dataMap.put("image", getImageStr(imgPath.get(0),getRootPath,getbaseUrl));
	 	        dataMap.put("image2", "");
	 	        dataMap.put("image3", "");
	        }
	        if(imgPath.size()<=0){
	        	dataMap.put("image", "请上传图片");
	 	        dataMap.put("image2", "请上传图片");
	 	        dataMap.put("image3", "请上传图片");
	        }
          Configuration configuration = new Configuration();  
          configuration.setDefaultEncoding("utf-8");  
          configuration.setClassForTemplateLoading(this.getClass(),"/xml");  //本地路径，模板备份在项目/xml下
          SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
          Date date = new Date();
          String resId = "GTE"+format.format(date);
          String filePath = DateUtil.DateToString(date, "YYYY/MM/dd");
          File f = new File(getRootPath +"myword/"+filePath);
		  if (!f.exists()){
		  	f.mkdirs();
		  }
	  	  StringBuffer tempfilePathBuffer = new StringBuffer();          //生成位置
		  tempfilePathBuffer.append(getRootPath).append("myword/").
		  append(filePath).append("/").append(resId).append(".doc");
		  String tempfilePath = tempfilePathBuffer.toString();				
		  StringBuffer wordUrlBuffer = new StringBuffer();					//生成URL
		  wordUrlBuffer.append(getbaseUrl).append("myword/").
		  append(filePath).append("/").append(resId).append(".doc");
		  String wordUrl = wordUrlBuffer.toString();
		  File outFile = new File(tempfilePath);  							//设置位置
          //File outFile = new File("D:\\testWORDFTL.doc");  
          Template t =  configuration.getTemplate("gdtask.ftl","utf-8");  
          Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"),10240);  
          t.process(dataMap, out);  
          out.close();
          return wordUrl;
    }
    
    private static String getImageStr(String imgPath,String getRootPath,String getbaseUrl) {
        String imgFile = getRootPath+imgPath.replace(getbaseUrl,"");  //服务器
		//String imgFile = ("D:\\suc.png");							//本地测试
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

}
